<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use app\admin\model\User;
use app\common\controller\Backend;
use app\common\model\Attachment;
use fast\Date;
use think\Db;

/**
 * 控制台
 *
 * @icon   fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {
        try {
            \think\Db::execute("SET @@sql_mode='';");
        } catch (\Exception $e) {

        }
        $column = [];
        $starttime = Date::unixtime('day', -6);
        $endtime = Date::unixtime('day', 0, 'end');
        $joinlist = Db("user")->where('jointime', 'between time', [$starttime, $endtime])
            ->field('jointime, status, COUNT(*) AS nums, DATE_FORMAT(FROM_UNIXTIME(jointime), "%m-%d") AS join_date')
            ->group('join_date')
            ->select();
        for ($time = $starttime; $time <= $endtime;) {
            $column[] = date("Y-m-d", $time);
            $time += 86400;
        }
        $userlist = array_fill_keys($column, 0);
        foreach ($joinlist as $k => $v) {
            $userlist[$v['join_date']] = $v['nums'];
        }

        $manage_community = Db::name("admin")->where(['id'=>$this->auth->id])->value("manage_community");
        $w = [];
        $ww = [];
        if($manage_community > 0){
            $manage_community = Db::name("admin")->where(['id'=>$this->auth->id])->value("manage_community");
            $house_ids = Db::name("house")->where(['community'=>$manage_community])->column("id");
            $w['community'] = $manage_community;
            $ww['house_id'] = ['in',$house_ids];
        }
        $column = [];
        $starttime = Date::unixtime('day', -8);
        $endtime = Date::unixtime('day', 0, 'end');
        $joinlist_room = Db("house")->where($w)->where('create_time', 'between time', [$starttime, $endtime])
            ->field('create_time, status, COUNT(*) AS nums, DATE_FORMAT(FROM_UNIXTIME(create_time), "%Y-%m-%d") AS join_date')
            ->group('join_date')
            ->select();
        //echo Db("building_room")->getLastSql();
        for ($time = $starttime; $time <= $endtime;) {
            $column[] = date("Y-m-d", $time);
            $time += 86400;
        }
        $roomlist = array_fill_keys($column, 0);
        foreach ($joinlist_room as $k => $v) {
            $roomlist[$v['join_date']] = $v['nums'];
        }

        $column = [];
        $starttime = Date::unixtime('day', -8);
        $endtime = Date::unixtime('day', 0, 'end');
        $joinlist_contract = Db("user_contract")->where($ww)->where('createtime', 'between time', [$starttime, $endtime])
            ->field('createtime, COUNT(*) AS nums, DATE_FORMAT(FROM_UNIXTIME(createtime), "%Y-%m-%d") AS join_date')
            ->group('join_date')
            ->select();
        //echo Db("building_room")->getLastSql();
        for ($time = $starttime; $time <= $endtime;) {
            $column[] = date("Y-m-d", $time);
            $time += 86400;
        }
        $contractlist = array_fill_keys($column, 0);
        foreach ($joinlist_contract as $k => $v) {
            $contractlist[$v['join_date']] = $v['nums'];
        }

        $dbTableList = Db::query("SHOW TABLE STATUS");
        $addonList = get_addon_list();
        $totalworkingaddon = 0;
        $totaladdon = count($addonList);
        foreach ($addonList as $index => $item) {
            if ($item['state']) {
                $totalworkingaddon += 1;
            }
        }



        //房源总数
        $totalroom = Db::name("house")->where($w)->count();
        //浏览数
        $visit_num = Db::name("house")->where($w)->sum("visit_num");
        //日成交量
        $daytime = strtotime(\date('Y-m-d',time()));
        $totalday = Db::name("user_contract")->where($ww)->where(['createtime'=>['>',$daytime]])->count();
        //月成交量
        $mtime = strtotime(\date('Y-m-01',time()));
        $totalm = Db::name("user_contract")->where($ww)->where(['createtime'=>['>',$mtime]])->count();
        //echo Db::name("user_contract")->getLastSql();
        //年成交量
        $ytime = strtotime(\date('Y-01-01',time()));
        $totaly = Db::name("user_contract")->where($ww)->where(['createtime'=>['>',$ytime]])->count();
        //echo Db::name("user_contract")->getLastSql();
        //充值总数
        //var_dump($roomlist);
        $recharge_money = Db::name("user_recharge")->where(['status'=>1])->sum("money");
        $this->view->assign([
            'totalroom'         => $totalroom,
            'manage_community'         => $manage_community,
            'visit_num'         => $visit_num,
            'totalday'         => $totalday,
            'totalm'         => $totalm,
            'totaly'         => $totaly,
            'recharge_money'         => $recharge_money,
            'totaluser'         => User::count(),
            'totaladdon'        => $totaladdon,
            'totaladmin'        => Admin::count(),
            'totalcategory'     => \app\common\model\Category::count(),
            'todayusersignup'   => User::whereTime('jointime', 'today')->count(),
            'todayuserlogin'    => User::whereTime('logintime', 'today')->count(),
            'sevendau'          => User::whereTime('jointime|logintime|prevtime', '-7 days')->count(),
            'thirtydau'         => User::whereTime('jointime|logintime|prevtime', '-30 days')->count(),
            'threednu'          => User::whereTime('jointime', '-3 days')->count(),
            'sevendnu'          => User::whereTime('jointime', '-7 days')->count(),
            'dbtablenums'       => count($dbTableList),
            'dbsize'            => array_sum(array_map(function ($item) {
                return $item['Data_length'] + $item['Index_length'];
            }, $dbTableList)),
            'totalworkingaddon' => $totalworkingaddon,
            'attachmentnums'    => Attachment::count(),
            'attachmentsize'    => Attachment::sum('filesize'),
            'picturenums'       => Attachment::where('mimetype', 'like', 'image/%')->count(),
            'picturesize'       => Attachment::where('mimetype', 'like', 'image/%')->sum('filesize'),
        ]);

        $this->assignconfig('column', array_keys($userlist));
        $this->assignconfig('roomcolumn', array_keys($roomlist));
        $this->assignconfig('contractcolumn', array_keys($contractlist));
        $this->assignconfig('userdata', array_values($userlist));
        $this->assignconfig('roomdata', array_values($roomlist));
        $this->assignconfig('contractdata', array_values($contractlist));

        return $this->view->fetch();
    }

}
